package com.interest.modules.task.service.impl;

import com.interest.modules.task.dao.TaskMapper;
import com.interest.modules.task.entity.Task;
import com.interest.modules.task.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("taskService")
public class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskMapper taskMapper;

    @Override
    public int delete(int id) {
        return taskMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int add(Task task) {
        // todo: should this be implement within an isolate AOP
        if (null == task) {
            return -1;
        }

        return taskMapper.insert(task);
    }

    @Override
    public Task getDetail(int id) {
        return taskMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Task> getList() {
        return taskMapper.selectList();
    }

    @Override
    public int update(Task task) {
        // todo: should this be implement within an isolate AOP
        if (null == task) {
            return -1;
        }

        return taskMapper.updateByPrimaryKeyWithBLOBs(task);
    }

    @Override
    public int complete(int id) {
        Task task = getDetail(id);
        task.setCompleted(true);
        return update(task);
    }

    @Override
    public int activate(int id) {
        Task task = getDetail(id);
        task.setCompleted(false);
        return update(task);
    }

}
